<template>
  <div class="image-link-container">
    <!-- 图片区域 -->
    <div 
      class="image-wrapper"
      :style="{ height: property.imageHeight + 'px' }"
      @click="handleImageClick"
    >
      <img 
        :src="property.imageUrl" 
        :alt="property.description || '图片'"
        class="image-content"
        @error="handleImageError"
      />
      
      <!-- 加载失败提示 -->
      <div v-if="imageError" class="image-error">
        <Icon icon="ep:picture" :size="32" />
        <span>图片加载失败</span>
      </div>
    </div>
    
    <!-- 描述文字 -->
    <div 
      v-if="property.showDescription && property.description" 
      class="description"
      :style="{
        color: property.descriptionColor,
        fontSize: property.descriptionSize + 'px'
      }"
    >
      {{ property.description }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ImageLinkProperty } from './config'

defineOptions({ name: 'ImageLink' })

const props = defineProps<{
  property: ImageLinkProperty
}>()

// 图片加载错误状态
const imageError = ref(false)

// 处理图片点击
const handleImageClick = () => {
  if (props.property.linkUrl) {
    if (props.property.openInNewTab) {
      window.open(props.property.linkUrl, '_blank')
    } else {
      window.location.href = props.property.linkUrl
    }
  }
}

// 处理图片加载错误
const handleImageError = () => {
  imageError.value = true
}
</script>

<style scoped lang="scss">
.image-link-container {
  width: 100%;
  cursor: pointer;
  
  .image-wrapper {
    position: relative;
    width: 100%;
    overflow: hidden;
    border-radius: 8px;
    background-color: #f5f5f5;
    
    .image-content {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;
    }
    
    .image-error {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100%;
      color: #999;
      font-size: 12px;
      
      span {
        margin-top: 8px;
      }
    }
    
    &:hover .image-content {
      transform: scale(1.05);
    }
  }
  
  .description {
    margin-top: 8px;
    text-align: center;
    line-height: 1.4;
    word-break: break-all;
  }
}
</style>

